/**
 *和地点相关的
 */
(function($){
	
	/**
	 * 主函数执行
	 */
	$(function(){
	   /**
	    * 根据省份的变动获取城市
	    */
		$("#province").change(function(){ 
			var oilBrandId = $(this).attr("oilBrandId");
			if(oilBrandId && oilBrandId !="") {
				changeCityByProvince($(this).val(),oilBrandId);
			}else {
				changeCityByProvince($(this).val(),"");
			}
			
	    });
	   /**
	    * 根据城市的变动获取县
	    */
	   $("#cityId,#city").change(function(){ 
	   	   var excute = $(this).attr("execute");
	   	   if(excute === "false") {
	   	   	  return ;
	   	   }
	   	   chageCountyByCity($(this).val());
	   });
   }); 
	/**
	 * 根据省份改变城市
	 */
	function changeCityByProvince(id,oilBrandId) {
		var d = {"id":id,oilBrandId:oilBrandId};
        var webRoot = $("#webRoot").val();
        var url = webRoot+"/cms/manage/area/city/getCityByProvince";
        $.post(url,d,function(data){
            var node = "";
            for(var i = 0;i<data.length;i++) {
               var dd = data[i];
               //判断是否也要获取县一级的信息
               if(i==0) {
               	  if($("select#countyId").size()>0) {
               	  	chageCountyByCity(dd.id);
               	  }
               }
               node+="<option value='"+dd.id+"'>"+dd.name+"</option>"; 
            }
            $("#cityId,#city").html(node);
        },"json");
    }
    /**
     * 根据城市改变县
     */
     function chageCountyByCity(id) {
     	var d = {"id":id}
        var webRoot = $("#webRoot").val();
        var url = webRoot+"/cms/manage/area/county/getCountyByCity";
        $.post(url,d,function(data){
            var node = "";
            for(var i = 0;i<data.length;i++) {
               var dd = data[i];
               node+="<option value='"+dd.id+"'>"+dd.name+"</option>"; 
               if($("select.factorys").size()>0) {
               	 chageFactoryByCounty(dd.id);
               }
            }
            $("select#countyId").html(node);
        },"json");
     }
     function chageFactoryByCounty(id) {
     	var d = {"countyId":id};
     	var webRoot = $("#webRoot").val();
     	var url = webRoot+"/cms/manage/factory/factory/listFactory";
         $.post(url,d,function(data){
            var node = "";
            for(var i = 0;i<data.length;i++) {
               var dd = data[i];
               node+="<option value='"+dd.id+"'>"+dd.name+"</option>"; 
            }
            $("select.factorys").html(node);
        },"json");
    }
})(jQuery)

